import ajax from '../../utils/request'
Page({
    data: {
        imgUrls: [],
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        circular: true,
        filmList: [],
        active: 0,
        currPage: 1 // 添加 currPage 状态
    },
    onShow: function () {
        this.getList();
    },
    onChange(event) {
        // 切换类型时，重置 currPage 为 1
        this.setData({
            currPage: 1
        });
        let data = {
            currPage: 1,
            type: event.detail.name
        }
        this.getList(data);
    },
    async getList(data = {
        currPage: this.data.currPage, 
        type: 1
    }) {
        try {
            // 发起请求
            let res = await ajax('http://43.136.85.99:3008/film/list', data);
            let swiperRes = await ajax('http://43.136.85.99:3008/film/banners', {});

            if (data.currPage === 1) {
                // 如果是第一页，直接替换 filmList
                this.setData({
                    filmList: res,
                    imgUrls: swiperRes
                });
            } else {
                // 如果不是第一页，将新数据追加到 filmList 中
                this.setData({
                    filmList: this.data.filmList.concat(res),
                    imgUrls: swiperRes
                });
            }
        } catch (error) {
            console.error('请求数据时出现错误:', error);
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // 页面滑动到底部时，将 currPage 加 1
        let newCurrPage = this.data.currPage + 1;
        this.setData({
            currPage: newCurrPage
        });
        let data = {
            currPage: newCurrPage,
            type: this.data.active
        };
        this.getList(data);
    }
})